package com.transaction.demo.base.biz.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import lombok.extern.slf4j.Slf4j;
import static com.transaction.demo.base.enums.ExceptionEnum.DICT_ITEM_EXIST_ERROR;
import static com.transaction.demo.base.enums.ExceptionEnum.DICT_ITEM_NOT_EXIST_ERROR;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.transaction.demo.base.exception.BaseServiceException;
import com.transaction.demo.base.biz.SysDictItemBiz;
import com.transaction.demo.base.dao.SysDictItemDao;
import com.transaction.demo.base.entity.SysDictItemBean;
import com.transaction.demo.base.vo.req.SysDictItemAddReqVo;
import com.transaction.demo.base.vo.req.SysDictItemListReqVo;
import com.transaction.demo.base.vo.req.SysDictItemReqVo;
import com.transaction.demo.base.vo.req.SysDictItemUpdateReqVo;
import com.transaction.demo.base.vo.resp.SysDictItemRespVo;
import com.transaction.demo.common.util.ResultPage;

/**
 * 描述：字典数据表
 * 作者：马庆智
 * 日期：2023/03/28 17:07:14
 */
@Slf4j
@Service
public class SysDictItemBizImpl implements SysDictItemBiz {

	@Autowired
	private SysDictItemDao sysDictItemDao;

	/**
	 * 查询字典数据表
	 *
	 * @param sysDictItem 请求参数
	 * @return SysDictItemResp
	 * @author 马庆智
	 * @date: 2023/03/28 17:07:14
	 */
	@Override
	public SysDictItemRespVo querySysDictItem(SysDictItemReqVo sysDictItem) {

		SysDictItemBean query = sysDictItemDao.querySysDictItem(SysDictItemBean.builder().build());

		return SysDictItemRespVo.builder().build();
	}

	/**
	 * 查询字典数据表列表
	 *
	 * @param sysDictItem 请求参数
	 * @return SysDictItemResp
	 * @author 马庆智
	 * @date: 2023/03/28 17:07:14
	 */
	@Override
	public ResultPage<SysDictItemRespVo> querySysDictItemList(SysDictItemListReqVo sysDictItem) {

		PageHelper.startPage(sysDictItem.getCurrent(), sysDictItem.getPageSize());
		List<SysDictItemBean> query = sysDictItemDao.querySysDictItemList(
				SysDictItemBean.builder().dictTypeCode(sysDictItem.getDictTypeCode())
						.dictItemName(sysDictItem.getDictItemName()).status(sysDictItem.getStatus()).build());
		PageInfo<SysDictItemBean> pageInfo = new PageInfo<>(query);

		List<SysDictItemRespVo> list = pageInfo.getList().stream().map(x -> {
			SysDictItemRespVo resp = new SysDictItemRespVo();
			BeanUtils.copyProperties(x, resp);
			return resp;
		}).collect(Collectors.toList());

		return new ResultPage<>(list, pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal());

	}

	/**
	 * 添加字典数据表
	 *
	 * @param addReq 请求参数
	 * @return int
	 * @author 马庆智
	 * @date: 2023/03/28 17:07:14
	 */
	@Override
	public int saveSysDictItem(SysDictItemAddReqVo addReq) {
		SysDictItemBean dictItem = new SysDictItemBean();
		dictItem.setDictItemName(addReq.getDictItemName());

		SysDictItemBean sysDictItemBean = sysDictItemDao.querySysDictItem(dictItem);
		if (sysDictItemBean == null) {
			log.error("字典数据：{}已存在", addReq);
			throw new BaseServiceException(DICT_ITEM_EXIST_ERROR);
		}

		BeanUtils.copyProperties(addReq, dictItem);
		dictItem.setCreatedBy("koobe");
		dictItem.setCreatedDate(new Date());
		return sysDictItemDao.saveSysDictItem(dictItem);
	}

	/**
	 * 删除字典数据表
	 *
	 * @param ids 请求参数
	 * @return int
	 * @author 马庆智
	 * @date: 2023/03/28 17:07:14
	 */
	@Override
	public int deleteSysDictItem(String ids) {
		return sysDictItemDao.deleteSysDictItem(
				Arrays.stream(ids.split(",")).map(Integer::parseInt).collect(Collectors.toList()));
	}

	/**
	 * 更新字典数据表
	 *
	 * @param updateReq 请求参数
	 * @return int
	 * @author 马庆智
	 * @date: 2023/03/28 17:07:14
	 */
	@Override
	public int updateSysDictItem(SysDictItemUpdateReqVo updateReq) {
		SysDictItemBean dictItem = new SysDictItemBean();
		dictItem.setDictItemId(updateReq.getDictItemId());

		SysDictItemBean sysDictItemBean = sysDictItemDao.querySysDictItem(dictItem);
		if (sysDictItemBean == null) {
			log.error("字典数据：{}不存在", updateReq);
			throw new BaseServiceException(DICT_ITEM_NOT_EXIST_ERROR);
		}

		BeanUtils.copyProperties(updateReq, dictItem);
		dictItem.setUpdatedBy("koobe");
		dictItem.setUpdatedDate(new Date());
		return sysDictItemDao.updateSysDictItem(dictItem);
	}

}